package com.lagou.controller;


import com.github.pagehelper.PageInfo;
import com.lagou.domain.ResponseResult;
import com.lagou.domain.Role;
import com.lagou.domain.User;
import com.lagou.domain.UserVO;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*用户分页&多条件查询方法*/
    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVO userVO) {

        PageInfo pageInfo = userService.findAllUserByPage(userVO);
        ResponseResult responseResult = new ResponseResult(true,200,"响应成功",pageInfo);
        List<User> list = pageInfo.getList();
        System.out.println(list);
        return responseResult;
    }

    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {

        User user1 = userService.login (user);

        if (user1 != null) {

            //保存用户id及access_toke到session中
            HttpSession session = request.getSession ();
            String access_toke = UUID.randomUUID ().toString ();
            session.setAttribute ("access_toke",access_toke);
            session.setAttribute ("user_id",user1.getId ());

            //将查询出来的信息响应给前台
            Map<String,Object> map = new HashMap<> ();
            map.put ("access_toke",access_toke);
            map.put("user_id",user1.getId ());

            //将查询出来的user也存到map中
            map.put ("user",user1);

            return new ResponseResult (true,200,"登录成功",map);

        } else {
            return new ResponseResult (true,400,"用户名密码错误",null);
        }
    }

    @RequestMapping("/findUserRelationRoleById")
    public ResponseResult findUserRelationRoleById(Integer id) {

        List<Role> userRelationRoleById = userService.findUserRelationRoleById (id);

        return new ResponseResult (true,200,"响应成功",userRelationRoleById);
    }

    /*分配角色*/
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVO userVO) {

        userService.userContextRole (userVO);

        return new ResponseResult (true,200,"响应成功",null);
    }

    /*获取用户权限，进行动态展示*/
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {

        //1.获取请求头中的token
        String authorization = request.getHeader ("Authorization");

        //2.获取session中的token
        String access_token = (String) request.getSession ().getAttribute ("access_token");

        //3.判断token是否一致
        if (authorization.equals (access_token)) {
            //获取用户id
            Integer user_id = (Integer) request.getSession ().getAttribute ("user_id");
            //获取session中的access_token
            ResponseResult responseResult = userService.getUserPermissions (user_id);
            return responseResult;
        } else {
            return new ResponseResult (true,200,"用户或密码错误",null);
        }

    }
}
